/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RemoteDesktop;

import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDesktopPane;
import javax.swing.JPanel;

/**
 *
 * @author Oho
 */
public class ClientCommand implements KeyListener,
        MouseMotionListener, MouseListener {

    Socket sk;
    JPanel pn;
    PrintWriter writer;
    Rectangle rt;

    public ClientCommand(Socket sk, JPanel pn, Rectangle rt) {
        try {
            this.sk = sk;
            this.pn = pn;
            this.rt = rt;
            this.pn.addKeyListener(this);
            this.pn.addMouseListener(this);
            this.pn.addMouseMotionListener(this);
            writer = new PrintWriter(sk.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(ClientCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

      public void mouseDragged(MouseEvent e) {
          mouseMoved(e);
          
    }

    public void mouseMoved(MouseEvent e) {
        double xScale = rt.getWidth()/pn.getWidth();
        System.out.println("xScale: " + xScale);
        double yScale = rt.getHeight()/pn.getHeight();
        System.out.println("yScale: " + yScale);
        System.out.println("Mouse Moved");
        writer.println(EnumCommands.MOVE_MOUSE.getAbbrev());
        writer.println((int)(e.getX() * xScale));
        writer.println((int)(e.getY() * yScale));
        writer.flush();
    }

    //this is not implemented
    public void mouseClicked(MouseEvent e) {
       // writer.print(e.getButton());
        
    }

    public void mousePressed(MouseEvent e) {
        System.out.println("Mouse Pressed");
        writer.println(EnumCommands.PRESS_MOUSE.getAbbrev());
        int button = e.getButton();
        int xButton = 16;
        if (button == 3) {
            xButton = 4;
        }
        writer.println(xButton);
        writer.flush();
    }

    public void mouseReleased(MouseEvent e) {
        System.out.println("Mouse Released");
        writer.println(EnumCommands.RELEASE_MOUSE.getAbbrev());
        int button = e.getButton();
        int xButton = 16;
        if (button == 3) {
            xButton = 4;
        }
        writer.println(xButton);
        writer.flush();
    }

    //not implemented
    public void mouseEntered(MouseEvent e) {
    }

    //not implemented
    public void mouseExited(MouseEvent e) {

    }

    //not implemented
    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        System.out.println("Key Pressed");
        writer.println(EnumCommands.PRESS_KEY.getAbbrev());
        writer.println(e.getKeyCode());
        writer.flush();
    }

    public void keyReleased(KeyEvent e) {
        System.out.println("Mouse Released");
        writer.println(EnumCommands.RELEASE_KEY.getAbbrev());
        writer.println(e.getKeyCode());
        writer.flush();
    }

}
